package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.*;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-06-16
 */
@Api(tags = "基础设置-房间管理")
@RestController
@RequestMapping("/admin/auth/fangjian")
public class FangjianController {
    @Resource
    private FangjianService fangjianService;
    @Resource
    private LoucengService loucengService;
    @Resource
    private YlAdminService ylAdminService;
    @Resource
    private FangjianleixingService fangjianleixingService;
    @Resource
    private LaorenService laorenService;
    @Resource
    private ChuangweiService chuangweiService;



    @ApiOperation(value = "床位全查+分页")
    @GetMapping("/findPageFangjian/{page}/{limit}")
    public Result findPageFangjian(@PathVariable @ApiParam("当前页") Integer page, @PathVariable @ApiParam("总条数") Integer limit){
        Page<Fangjian> pagefarm = new Page<>(page,limit);
        IPage<Fangjian> page1 = fangjianService.page(pagefarm);
        for (Fangjian fangjian:page1.getRecords()){
            //对楼层进行全查
            List<Louceng> list=loucengService.list();
            //对创建人进行全查
            List<YlAdmin> list2 = ylAdminService.list();
            //对房间类型进行全查
            List<Fangjianleixing> list3 = fangjianleixingService.list();
            //对老人进行全查
            List<Laoren> list4=laorenService.list();
            //对床位进行全查
            List<Chuangwei> list5=chuangweiService.list();
            for (Louceng louceng : list) {
                if(fangjian.getFloorId()==louceng.getId()){
                    fangjian.setCoucengname(louceng.getName());
                }
            }
            for (YlAdmin ylAdmin : list2) {
                if (fangjian.getAdminId() == ylAdmin.getId()) {
                    fangjian.setAdminname(ylAdmin.getName());
                }
            }
            //循环房间类型
            for (Fangjianleixing fangjianleixing : list3) {
                if (fangjian.getRoomType() == fangjianleixing.getId()) {
                    fangjian.setFangjiantype(fangjianleixing.getTypeName());
                    fangjian.setTypecount(fangjianleixing.getBedNumber());
                }
            }
            //循环床位
            String nameman="";
            int count = 0; // 计数器，用于记录已取出的老人名字数量
            for(Chuangwei chuangwei: list5){
                if(fangjian.getId()==chuangwei.getRoomId()){
                    for(Laoren laoren:list4){
                        if(chuangwei.getId()==laoren.getBedId()) {
                            if (laoren.getStatus() == 2) {
                                chuangwei.setMannametwo(laoren.getName());
                                fangjian.setManname(chuangwei.getMannametwo());
                                if (count > 0) { // 如果不是第一个老人名字，则加上 "、" 符号
                                    nameman += "、";
                                }
                                nameman += laoren.getName(); // 拼接老人名字
                                count++; // 计数器加1

                            }
                        }
                    }
                    if (!nameman.isEmpty()) { // 如果名字不为空，则去掉最后一个逗号和空格，并设置到房间中
                        for (Fangjianleixing fangjianleixing : list3) {
                            if (fangjian.getRoomType() == fangjianleixing.getId()) {
                                fangjian.setFangjiantype(fangjianleixing.getTypeName());
                                fangjian.setTypecount(fangjianleixing.getBedNumber());
                                fangjian.setTypecount(count+"/"+fangjian.getTypecount());
                            }
                        }
                        fangjian.setManname(nameman);

                    }
                }
                System.out.println("----------------------");
                System.out.println(nameman);
                System.out.println(count);
            }
        }
        return Result.ok(page1);
    }
    //添加房间-----------------------------------------------------
    @ApiOperation(value = "添加房间")
    @PostMapping("/save")
    public Result save(@RequestBody Fangjian fangjian){
        LocalDateTime now = LocalDateTime.now();
        long r=1;
        fangjian.setAdminId(r);
        fangjian.setCreateDate(now);
        fangjianService.save(fangjian);
        return Result.ok();
    }
    //删除房间-----------------------------------------------------
    @ApiOperation(value = "删除房间")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        fangjianService.removeById(id);
        return Result.ok();
    }
    @ApiOperation(value = "根据id查询房间")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Fangjian fangjian=fangjianService.selectById(id);
        return Result.ok(fangjian);
    }
    @ApiOperation(value = "根据id查询房间回显")
    @GetMapping("/getfangById/{id}")
    public Result getfangById(@PathVariable Long id){
        Fangjian fangjian=fangjianService.getById(id);
        return Result.ok(fangjian);
    }
    @ApiOperation(value = "修改房间")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody Fangjian fangjian){
        fangjianService.updateById(fangjian);
        return Result.ok();
    }
    @ApiOperation(value = "查询回显房间")
    @GetMapping("/getfangjian")
    public Result getfangjian(){
        List<Fangjian> fangjian = fangjianService.list();
        return Result.ok(fangjian);
    }
}

